import datetime
import os
import sys
import wmi #wmi relies on pywin32 adn wmi

s=wmi.WMI()


#https://learn.microsoft.com/zh-cn/windows/win32/cimwin32prov


#cpu信息
def get_CPU_info():
    cpuinfo=[]
    cpuo=s.Win32_Processor()
    for u in cpuo:
        cpuinfo.append(
            {
                "Device": "CPU",
                "Name":u.name,
                "SN":u.ProcessorId,
                "CoreNum":u.NumberOfCores,
                "ThreadNum":u.ThreadCount
            }
        )
    return cpuinfo
    
#硬盘信息
def get_disk_info():
    diskinfo=[]
    disknum=0
    for disk in s.Win32_DiskDrive():
        diskinfo.append(
            {
                "Device": "硬盘",
                "SN":s.Win32_PhysicalMedia()[disknum].SerialNumber,
                "Caption":disk.Caption,
                "size":(str(int(float(disk.Size)/1024/1024/1024))+"G") if disk.Size!=None else "unknown"
            }
        )
        disknum+=1
    return diskinfo


#主板信息
def get_baseboard_info():
    baseboardinfo=[]
    for board in s.Win32_BaseBoard():
        baseboardinfo.append({
            "Device": "主板",
            "SN":board.SerialNumber,
            "Model":board.Product,
            "Productor":board.Manufacturer
        })
    return baseboardinfo


#内存信息
def get_memory_info():
    memoryinfo=[]
    for mem in s.Win32_PhysicalMemory():
        memoryinfo.append(
            {
                "Device": "内存",
                "Productor":mem.Manufacturer,
                "Speed":mem.ConfiguredClockSpeed,
                "PN":mem.PartNumber,
                "SN":mem.SerialNumber,
                "Size":str(int(float(mem.Capacity)/1024/1024/1024))+"G",
                "BankLabel":mem.BankLabel
            }
        )
    return memoryinfo



#显卡信息
def get_gpu_info():
    gpuinfo=[]
    for g in s.Win32_VideoController():
        gpuinfo.append({
            "Device": "显卡",
            "Name":g.VideoModeDescription,
            "Description":g.Caption
        })
    return gpuinfo


#IP/MAC
def get_net_info():
    netinfo=[]
    for net in s.Win32_NetworkAdapterConfiguration():
        if net.IPEnabled==True:
            for addr in net.IPAddress:
                if (addr != "" ):
                    netinfo.append({
                        "Device": "IP/MAC",
                        "Name":net.ServiceName,
                        "Address":addr,
                        "MAC":net.MACAddress
                    })
                    break
    return netinfo


def get_OS_info():
    osinfo=[]
    for sos in s.Win32_OperatingSystem():
        osinfo.append({
            "Device": "系统",
            "Name":sos.Caption
        })
    return osinfo

print("该程序仅用于登记固定资产信息，不会收集其他信息收集\n")

username=input("请输入你的名称:")


info_list=[]
info_list.append(get_CPU_info())
info_list.append(get_baseboard_info())
info_list.append(get_disk_info())
info_list.append(get_gpu_info())
info_list.append(get_memory_info())
info_list.append(get_OS_info())
info_list.append(get_net_info())

with open("-"+username+datetime.datetime.now().__format__("-%Y%m%d%H%M%S")+".log","w+",encoding="utf-8") as f:
    for i in info_list:
        for j in i:
            for l in j:
                f.write(l+" : "+str(j[l]) +"\n")
        f.write("\n")
